import 'package:flutter/material.dart';

import 'type.dart';

class PlanedListView extends StatelessWidget {
  const PlanedListView({
    Key? key,
    this.sectionCount = 1,
    this.sectionHeight,
    required this.itemCount,
    this.sectionBuilder,
    required this.itemBuilder,
    this.header,
    this.footer,
    this.controller,
    this.physics,
    this.shrinkWrap = false,
    this.separatorIndent,
    this.padding,
    this.onRefresh,
    this.onLoading,
    this.refreshTopInset,
    this.hasSeprator = true,
    this.sepratorColor,
  }) : super(key: key);
  final int sectionCount;
  final ItemCount itemCount;
  final SectionHeight? sectionHeight;
  final IndexedWidgetBuilder? sectionBuilder;
  final ItemBuilder itemBuilder;
  final Widget? header;
  final Widget? footer;
  final ScrollController? controller;
  final EdgeInsets? separatorIndent;
  final ScrollPhysics? physics;
  final bool? shrinkWrap;
  final VoidCallback? onRefresh;
  final VoidCallback? onLoading;
  final EdgeInsets? padding;
  final double? refreshTopInset;
  final bool hasSeprator;
  final Color? sepratorColor;

  @override
  Widget build(BuildContext context) {
    return getList(context);
  }

  Widget getList(BuildContext context) {
    List<Widget> items = [];
    if (header != null) {
      items.add(header!);
    }
    for (var i = 0; i < sectionCount; i++) {
      if (sectionHeight != null &&
          sectionHeight!(i) > 0 &&
          sectionBuilder != null) {
        items.add(
          SizedBox(
            height: sectionHeight!(i),
            child: sectionBuilder!(context, i),
          ),
        );
      }
      var _itemCount = itemCount(i);
      if (_itemCount > 0) {
        for (var j = 0; j < _itemCount; j++) {
          items.add(itemBuilder(i, j));
        }
      }
    }

    if (footer != null) {
      items.add(footer!);
    }
    return ListView(
      physics: physics,
      shrinkWrap: shrinkWrap ?? false,
      controller: controller,
      padding: padding,
      children: items,
    );
  }
}
